package com.example.common.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

// 自定义通用返回类
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse<T> {
    // 状态码
    private int code;
    // 讯息
    private String message;
    // 是否处理成功
    private boolean success;
    // 返回数据
    private T data;
    
    public ApiResponse(int code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }
    
    /**
     * 成功处理
     */
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(200, "操作成功", true, data);
    }
    
    /**
     * 发生异常
     */
    public static <T> ApiResponse<T> error(int code, String message) {
        return new ApiResponse<>(code, message, false, null);
    }
}
